import VueRouter from 'vue-router';
import $store from '../store';

//基础路由
const baseRoutes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    component: () => import('../views/login/login.vue'),
    name: 'loginView',
    hidden: true,
    meta: {},
  },
  // {
  //   path: '/adminlogin',
  //   component: () => import('../views/login/adminlogin.vue'),
  //   name: 'loginView',
  //   hidden: true,
  //   meta: {},
  // },
  {
    path: '/ssologinout',
    component: () => import('../views/login/ssologinout.vue'),
    name: 'ssologinoutView',
    hidden: true,
    meta: {},
  },
  {
    path: '/ssocallback',
    component: () => import('../views/login/ssocallback.vue'),
    name: 'ssocallbackView',
    hidden: true,
    meta: {},
    // props: route => ({ query: route.query.q })
  },

  // {
  //     //其他
  //     path: "*",
  //     redirect: '/login',
  // },
  {
    //404
    path: '/404',
    component: () => import('../views/errorpages/404'),
  },
  {
    //404
    path: '*',
    component: () => import('../views/errorpages/404'),
  }
];

function createRouter(userRoutes) {
  let allRoutes = [];
  if (userRoutes && userRoutes.length > 0) {
    allRoutes = [...userRoutes, ...baseRoutes];
  } else {
    allRoutes = baseRoutes;
  }
  return new VueRouter({
    mode: 'history',
    scrollBehavior: () => ({ y: 0 }), //路由切换时，滚动条回到原点
    routes: allRoutes,
  });
}

const router = createRouter([]); //创建路由

//重置路由
export function resetRouter(newRoutes) {
  let newRouter = createRouter(newRoutes);
  router.matcher = newRouter.matcher;
}

router.beforeEach((to, from, next) => {
  console.log("@@@to", to)
  // console.log("@@@to", to)
  // next();
//基础路由控制，增加基础路由后这里要增加数量
console.log("@@@login", $store.state.permission.login)
   if ($store.state.permission.login) {
     next();
   } else {
    $store.dispatch('permission/login', { username: 'admin' });
    // router.replace(to.path);
    next({ path: to.path, query: to.query, hash: to.hash })
  }
});
export default router;
